import router from '@/router'

export function toMain () {
  router.push({
    path: '/console'
  })
}

export function toLogin () {
  removeToken()

  router.replace({
    path: '/login',
    query: {
      redirect: encodeURIComponent(router.currentRoute.fullPath)
    }
  })
}

export function setToken (token) {
  localStorage.setItem('token', token)
}

export function getToken () {
  return localStorage.getItem('token')
}

export function removeToken () {
  localStorage.removeItem('token')
}

export function setUser (user) {
  return localStorage.setItem('user', JSON.stringify(user))
}

export function getUser () {
  return JSON.parse(localStorage.getItem('user'))
}

export function removeUser () {
  localStorage.removeItem('user')
}

export function isOnLine () {
  try {
    let isOnLine = window.navigator.onLine
    return isOnLine
  } catch (e) {
    return false
  }
}

export function removeItem (key) {
  localStorage.removeItem(key)
}

export function setItem (key, item) {
  return localStorage.setItem(key, JSON.stringify(item))
}

export function getItem (key) {
  return JSON.parse(localStorage.getItem(key))
}

// --------------------------------------------设置或者获取分页大小------------------------------------------------
export function getPageSize (key) {
  let map = {
    client: 20,
    desktop: 20,
    resource: 20
  }

  return map[key] || 20
}

let PAGE_MAP = 'page_map'
export function pageValue (pageKey, pageSize) {
  let id = getUser().Id
  let key = `${pageKey}-${id}`
  let pageMap = getItem(PAGE_MAP) || {}
  // 已存的分页值
  let currentPageSize = pageSize || pageMap[key] || getPageSize(pageKey)

  pageMap[key] = currentPageSize
  setItem(PAGE_MAP, pageMap)

  return currentPageSize
}
// --------------------------------------------设置或者获取分页大小------------------------------------------------
